package com.voja.mrvica2;

import android.app.Activity;
import android.app.ActivityManager;
import com.voja.AndroidUtils.Application;

import android.content.Context;
import android.graphics.Point;
import android.view.Display;
import android.view.WindowManager;

public class App extends Application {
    
    public static final int DO_EXCEPTION = 400;
    /**
     * cache lifetime in milliseconds
     */
    public static final int CACHE_LIFETIME = 10 * 60 * 1000; // 10 mins

    public static Cache cache;
    
    public static Display display;
    public static int     displayWidth;
    public static int     displayHeight;
    
    @Override
    public void onCreate() {
            super.onCreate();
            
            setContext(getApplicationContext());
            Application.DEFAULT_MESSAGE_TITLE = getContext().getResources()
                                                            .getString(R.string.app_name);
            
            initCache();
            initDisplaySize();
    }
    
    public void initCache() {
            ActivityManager am = (ActivityManager)getContext().getSystemService(Context.ACTIVITY_SERVICE);
            int size = am.getMemoryClass() * 1024 * 1024 / 8;
            App.cache = new Cache(size);
    }
    
    public void initDisplaySize() {
        Point p = new Point();
        display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
        display.getSize(p);
        
        displayWidth  = p.x;
        displayHeight = p.y;
    }
    
    public static void alert(String message) {
        Application.alert(message);
    }
    public static void error(String message) {
        String title = getContext().getResources().getString(R.string.app_name);
        int    icon  = android.R.drawable.ic_dialog_alert;
        Click  click = new Click("OK", new CommandWrapper(new Command() {
            @Override
            public void execute() {
                Activity activity = (Activity)getContext();
                activity.finish();
            }
        }));
        Application.alert(message, icon, title, click, null);
    }
}
